<template>
	<view>
		<!-- 聊天 -->
		<view class="indexTop">
			<image src="../../static/img/index_students.svg"></image>
			<text>聊天</text>
			<image></image>
		</view>

		<view class="search">
			<image src="../../static/img/chatSearch.svg"></image>
			<input type="text" placeholder="搜索昵称或者聊天记录">
			<view class="chatRight">
				<image src="../../static/img/chatAdd.png"></image>
				<!-- <image src="../../static/img/chatRight.svg"></image> -->
			</view>
		</view>

		<view class="chatContent">
			<view class="chatBox"  v-for="item in titleArr" :key="item.id" @click="chat(item.fid,item.img,item.name)">
				<image :src="item.img" mode="aspectFill"></image>
				<view class="title">
					<text class="name">{{item.name}}</text>
					<text class="titleCon">{{item.title}}</text>
					<text class="time">{{item.time}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				titleArr:[],
				userId: 0
			}

		},
		onShow() {
			this.join()
			this.getFriend()
		},
		onLoad() {
			uni.getStorage({
				key: 'userId',
				success: (res)=> {
					this.userId = JSON.parse(res.data).userId
				}
			})
			
		},
		methods: {
			async getFriend(){
				 await uni.request({
					url:'http://localhost:3000/chat/selFriend',
					method:"GET",
					data:{
						id:this.userId,
						},
					success: res =>{
						let arr = res.data.data.map(item =>{
							return {
								id:item.id,
								name:item.user_name,
								img:'http://localhost:3000/upload/'+ item.avatar,
								fid:item.friend_id,
								title: '我的聊天记录',
								time : "昨天",
							}
						})
						this.titleArr = arr
					}
				})
				
			},
			join() {
				this.socket.emit('login', this.userId)
			},
			// socketTest(){
			// 	this.socket.on('msg',id=>{
			// 		// console.log("id",id)
			// 	})
			// },
			chat(fid,img,name) {
				uni.navigateTo({
					url: `/pages/chatAction/chatAction?fid=${fid}&img=${img}&name=${name}`
				})
			}
		}

	}
</script>

<style lang="scss">
	.indexTop {
		width: 100%;
		height: 96rpx;
		line-height: 96rpx;
		text-align: center;
		border-bottom: 2rpx solid rgba(187, 187, 187, 0.5);
		margin-bottom: 8rpx;
		margin-top: 88rpx;

		// margin-top: var(--status-bar-height);
		image {
			float: left;
			width: 56rpx;
			height: 56rpx;
			margin-left: 40rpx;
			margin-top: 20rpx;
		}

		text {
			font-size: 40rpx;
		}

		image:nth-last-child(1) {
			float: right;
			background: #ccc;
			opacity: 0.2;
			border-radius: 56rpx;
			margin-right: 40rpx;
		}
	}

	.search {
		margin-left: 56rpx;
		position: relative;
		margin-bottom: 16rpx;

		// text-align: center;
		input {
			width: 576rpx;
			height: 88rpx;
			border: 2rpx solid rgba(187, 187, 187, 0.5);
			border-radius: 44rpx;
			box-sizing: border-box;
			line-height: 88rpx;
			padding-left: 128rpx;
			background-color: rgba(206, 206, 206, 0.1);
			font-size: 28rpx;
		}

		image:nth-child(1) {
			width: 56rpx;
			height: 56rpx;
			left: 32rpx;
			top: 16rpx;
			position: absolute;
		}

		.chatRight {
			position: absolute;
			top: 12rpx;
			right: 32rpx;
			width: 64rpx;
			height: 64rpx;
			border: 2rpx solid rgba(154, 154, 154, 1);
			border-radius: 80rpx;

			image {
				width: 48rpx;
				height: 48rpx;
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				margin: auto;
			}
		}
	}

	.chatContent {
		.chatBox {

			width: 100%;
			height: 156rpx;

			image {
				float: left;
				width: 104rpx;
				height: 104rpx;
				border-radius: 104rpx;
				margin-left: 32rpx;
				margin-top: 28rpx;
			}

			.title {
				float: right;
				position: relative;
				width: 594rpx;
				height: 156rpx;
				font-size: 28rpx;
				border-bottom: 4rpx solid #f5f5f5;
				overflow: hidden;

				.name {
					position: absolute;
					top: 40rpx;
					font-weight: bold;
					color: #202020;
				}

				.titleCon {
					position: absolute;
					top: 84rpx;
					color: #b5b5b5;
				}

				.time {
					font-size: 24rpx;
					color: #b5b5b5;
					position: absolute;
					right: 32rpx;
					top: 44rpx;
				}
			}

		}

	}
</style>
